using Lucene.Net.Documents;
using Webshop20.Constants;
using Webshop20.Domain.Shared.Products.Core;

namespace Webshop20.AntiCorruptionLayers.Lucene
{
    public class DocumentTranslator : IDocumentTranslator
    {
        public Document ConvertToDocument(Product product)
        {
            var document = new Document();
            document.Add(CreateNewField(CONSTANTS.KEYS.FULLTEXT_SEARCH_CONTENT_FIELD, product.Description));
            document.Add(CreateNewField(CONSTANTS.KEYS.FULLTEXT_SEARCH_CONTENT_FIELD, product.Name));
            document.Add(CreateNewField(CONSTANTS.KEYS.FULLTEXT_SEARCH_CONTENT_FIELD, product.PurchasOrderNumber));
            document.Add(CreateDocumentId(product.Id));

            return document;
        }

        Field CreateNewField(string name, string context)
        {
            return new Field(name,
                        context,
                        Field.Store.YES,
                        Field.Index.TOKENIZED,
                        Field.TermVector.YES);
        }

        Field CreateDocumentId(int id)
        {
            return new Field(CONSTANTS.KEYS.FULLTEXT_SEARCH_ID_FIELD,
             id.ToString(),
             Field.Store.YES,
             Field.Index.NO,
             Field.TermVector.NO);
        }
    }
}